package cn.wolfcode.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.*;
import cn.wolfcode.service.*;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.Request;

import cn.wolfcode.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private ISystemDictionaryItemService iSystemDictionaryItemService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IConsumptionItemService consumptionItemService;

    @Autowired
    private IEmployeeService employeeService;


    @Request(name="结算单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageResult", pageInfo);
        return "consumption/list";
    }


    @Request(name="结算单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }


    @Request(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @Request(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        List<Business> businesses = businessService.listAll();

        model.addAttribute("businesses",businesses);
        List<SystemDictionaryItem> systemDictionaryItems1 = iSystemDictionaryItemService.selectTypeId(1);
        List<SystemDictionaryItem> systemDictionaryItems0 = iSystemDictionaryItemService.selectTypeId(null);
        List<SystemDictionaryItem> systemDictionaryItems3 = iSystemDictionaryItemService.selectTypeId(3);
        model.addAttribute("systemDictionaryItems1",systemDictionaryItems1);
        model.addAttribute("systemDictionaryItems0",systemDictionaryItems0);
        model.addAttribute("systemDictionaryItems4",systemDictionaryItems3);



        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            Long payeeId = consumption.getPayee().getId();
            Employee employeePayee = employeeService.selectOne(payeeId);
            model.addAttribute("employeePayee",employeePayee);


            Long idAuditor = consumption.getAuditor().getId();
            Employee employeeAuditor = employeeService.selectOne(idAuditor);
            model.addAttribute("employeeAuditor",employeeAuditor);

            model.addAttribute("consumption",consumption);
            List<ConsumptionItem> consumptionItems = consumptionItemService.selectCno(consumption.getCno());
            System.out.println("=================="+consumption.toString());
            model.addAttribute("items",consumptionItems);

        }
        return "consumption/input";
    }

    @RequestMapping("/inputAppointment")
    public String inputAppointment(Long id,Model model,int status){
        appointmentService.saveStatus(status,id);
        Appointment appointment = appointmentService.get(id);



        Consumption consumption = new Consumption();

        consumption.setAppointmentAno(appointment.getAno());
        consumption.setCreateTime(appointment.getAppointmentTime());
        consumption.setBusiness(appointment.getBusinessId());
        consumption.setInfo(appointment.getInfo());
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        consumption.setBusiness(appointment.getBusinessId());

        String yyyyMMddHHmmss = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        String s = RandomUtil.randomNumbers(5);
        consumption.setCno(s+yyyyMMddHHmmss);

        consumption.setCreateUser(SessionUtil.getEmployee());


        consumptionService.save(consumption);
        return "redirect:/consumption/input?id="+consumption.getId();
    }

    public void selectType(Model model){

        List<SystemDictionaryItem> list = iSystemDictionaryItemService.selectConsumption();
        model.addAttribute("list",list);
    }

    @RequestMapping("/changeModel")
    public String changeModel(Long id,Model model){
        Employee employee = SessionUtil.getEmployee();
        Consumption consumption = consumptionService.get(id);
        consumption.setPayee(employee);
        consumption.setPayTime(new Date());
        consumption.setStatus(1);
        consumptionService.save1(consumption);
        model.addAttribute("employee",employee);
        return "redirect:/consumption/list";
    }

    @RequestMapping("/changeMode2")
    public String changeMode2(Long id,Model model,int status){
        Employee employee = SessionUtil.getEmployee();
        Consumption consumption = consumptionService.get(id);
        consumption.setPayee(employee);
        consumption.setAuditTime(new Date());
        consumption.setStatus(status);
        consumptionService.save1(consumption);
        model.addAttribute("employee",employee);
        return "redirect:/consumption/list";
    }

    @RequestMapping("/delete2")
    public String delete2(Long id) {
        if (id != null) {
            consumptionItemService.delete(id);
        }
        return "redirect:/consumption/list";
    }

    @RequestMapping("/deleteAll")
    public String deleteAll(Long[] ids) {
        for (Long id : ids) {
            if (id != null) {
                consumptionItemService.delete(id);
            }
        }

        return "redirect:/consumption/list";
    }

}
